package com.mzj.saas.jobs.lock.service.ruanku;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mzj.saas.commons.Constant;

import com.mzj.saas.commons.enums.LockAuthTypeEnum;
import com.mzj.saas.commons.enums.LockSuspendTypeEnum;
import com.mzj.saas.jobs.commons.RandomNoUtils;
import com.mzj.saas.jobs.commons.ResultBean;
import com.mzj.saas.jobs.commons.SSLNetProvider;
import com.mzj.saas.jobs.lock.service.sgin.RSAUtils;
import com.mzj.saas.jobs.lock.vo.DevMacStateVO;
import com.mzj.saas.jobs.lock.vo.DevMacVO;
import com.mzj.saas.jobs.lock.vo.DynamicPasswordVO;
import com.mzj.saas.jobs.lock.vo.yaluete.SmartLockVO;
import com.mzj.saas.jobs.redis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 软库-智能锁接口
 * 
 * @author hp
 *
 */
@Service
public class SmartLockService {

	private static final Logger log = LoggerFactory.getLogger(SmartLockService.class);

	@Autowired
	private RedisService redisService;

	/**
	 * 根据mac获取设备sn号
	 * 
	 * @param devMac
	 * @return 设备sn号
	 */
	public ResultBean<String> getSnByMac(String devMac) {
		try {
			if (StringUtils.isBlank(devMac)) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "device/V2/getSnByMac";
			SmartLockVO vo = new SmartLockVO();
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			vo.setDevMac(devMac);
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("获取设备sn号,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			if (!result.containsKey("code")) {
				return ResultBean.failedResultOfParamWithMsg(resultStr);
			}

			int code = Integer.parseInt(result.get("code").toString());
			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			String sn = result.get("data").toString();
			return ResultBean.successfulResult(sn);
		} catch (Exception e) {
			log.error("根据mac获取设备sn号异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 批量mac获取设备sn号
	 * 
	 * @param devMacs
	 * @return 批量设备sn号
	 */
	public ResultBean<List<DevMacVO>> batchGetSnByMac(List<String> devMacs) {
		try {
			if (CollectionUtils.isEmpty(devMacs)) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "device/V2/batchGetSnByMac";
			SmartLockVO vo = new SmartLockVO();
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			vo.setDevMacs(devMacs);
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("批量获取sn号,数据 = {},签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			if (!result.containsKey("code")) {
				return ResultBean.failedResultOfParamWithMsg(resultStr);
			}

			int code = Integer.parseInt(result.get("code").toString());
			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			List<DevMacVO> dataArray = JSONArray.parseArray(result.getString("data"), DevMacVO.class);
			return ResultBean.successfulResult(dataArray);
		} catch (Exception e) {
			log.error("批量根据mac获取设备sn号异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 获取锁状态
	 * 
	 * @param
	 * @return
	 */
	public ResultBean<DevMacStateVO> getState(String sn) {
		try {
			if (StringUtils.isBlank(sn)) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lock/V2/getState";
			SmartLockVO vo = new SmartLockVO();
			vo.setSn(sn);
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("获取锁状态,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			if (!result.containsKey("code")) {
				return ResultBean.failedResultOfParamWithMsg(resultStr);
			}

			int code = Integer.parseInt(result.get("code").toString());
			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			DevMacStateVO data = JSONObject.parseObject(result.getString("data"), DevMacStateVO.class);
			return ResultBean.successfulResult(data);
		} catch (Exception e) {
			log.error("获取锁状态异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 批量获取锁状态
	 * 
	 * @param
	 * @return
	 */
	public ResultBean<List<DevMacStateVO>> batchGetState(List<String> sns) {
		try {
			if (CollectionUtils.isEmpty(sns)) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lock/V2/getState";
			SmartLockVO vo = new SmartLockVO();
			vo.setSns(sns);
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("批量获取锁状态,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			if (!result.containsKey("code")) {
				return ResultBean.failedResultOfParamWithMsg(resultStr);
			}

			int code = Integer.parseInt(result.get("code").toString());
			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			List<DevMacStateVO> dataArray = JSONArray.parseArray(result.getString("data"), DevMacStateVO.class);
			return ResultBean.successfulResult(dataArray);
		} catch (Exception e) {
			log.error("批量获取锁状态异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 查询动态密码
	 * 
	 * @param vo
	 * @return
	 */
	public ResultBean<List<DynamicPasswordVO>> getDynamicPassword(SmartLockVO vo) {
		try {

			if (StringUtils.isBlank(vo.getSn()) || StringUtils.isBlank(vo.getStartTime())
					|| StringUtils.isBlank(vo.getEndTime())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/getDynamicPassword";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("查询动态密码,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			if (!result.containsKey("code")) {
				return ResultBean.failedResultOfParamWithMsg(resultStr);
			}

			int code = Integer.parseInt(result.get("code").toString());
			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			List<DynamicPasswordVO> dataArray = JSONArray.parseArray(result.getString("data"), DynamicPasswordVO.class);
			return ResultBean.successfulResult(dataArray);
		} catch (Exception e) {
			log.error("查询动态密码异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 新增/修改密码
	 * 
	 * @param
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> operationPassword(SmartLockVO vo) {

		try {
			if (StringUtils.isBlank(vo.getSn()) || vo.getIndex() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			// 临时密码不需要验证此参数
			if ((vo.getPasswordType() == null || vo.getPasswordType() == 1) && vo.getAuthInfoId() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			if (vo.getAuthType().equals(LockAuthTypeEnum.TIMES_PASS.getValue()) && vo.getTimes() == null) {
				return ResultBean.failedResultOfParamWithMsg("输入有效次数");
			}

			if (StringUtils.isBlank(vo.getStartTime()) || StringUtils.isBlank(vo.getEndTime())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/operationPassword";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("新增/修改密码,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));

			JSONObject result = JSONObject.parseObject(resultStr);

			if (!result.containsKey("code")) {
				return ResultBean.failedResultOfParamWithMsg(resultStr);
			}

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);
		} catch (Exception e) {
			log.error("新增/修改密码异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 冻结/解冻密码
	 * 
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> freezeOrUnfreeze(SmartLockVO vo) {
		try {
			log.info("软库, 智能锁冻结, vo={}", JSON.toJSONString(vo));
			if (StringUtils.isBlank(vo.getSn()) || vo.getSuspend() == null || vo.getIndex() == null
					|| vo.getAuthInfoId() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			if (!LockSuspendTypeEnum.operate(vo.getSuspend())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			if (vo.getAuthType().equals(LockAuthTypeEnum.TIMES_PASS.getValue()) && vo.getTimes() == null) {
				return ResultBean.failedResultOfParamWithMsg("输入有效次数");
			}

			// 冻结 冻结记录Id
			if (vo.getSuspend() == 1 && vo.getLockFrozenId() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/newFreezeOrUnfreeze";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("冻结/解冻密码,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);
		} catch (Exception e) {
			log.error("冻结/解冻密码异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 删除密码
	 * 
	 * @param vo
	 * @return 回调id
	 * 删除当前授权密码 缓存回调id
	 */
	public ResultBean<String> clearPassword(@RequestBody SmartLockVO vo) {
		try {
			//校验智能锁sn和密码序号
			if (StringUtils.isBlank(vo.getSn()) || vo.getIndex() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			// 临时密码不需要验证此参数
			if ((vo.getPasswordType() == null || vo.getPasswordType() == 1) && vo.getAuthInfoId() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			//智能锁访问令牌
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/clearPassword";
			vo.setAccessKey(accessKey);
			//获取6个随机数
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			//MD5生成签名
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("删除密码,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			//发送删除请求
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			//缓存智能锁 回调入参callBackId
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);
		} catch (Exception e) {
			log.error("删除密码异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 清除锁所有密码(不包含动态密码（动态密码可以继续使用）。)
	 * 
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> clearAllPassword(@RequestBody SmartLockVO vo) {
		try {

			if (StringUtils.isBlank(vo.getSn())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/clearAllPassword";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("清除锁所有密码,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);
		} catch (Exception e) {
			log.error("清除锁所有密码" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 新增/修改卡
	 * 
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> operationCardPassword(SmartLockVO vo) {
		try {
			if (StringUtils.isBlank(vo.getSn()) || vo.getCardIndex() == null || StringUtils.isBlank(vo.getCardId())
					|| vo.getAuthInfoId() == null || vo.getCardType() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			if (StringUtils.isBlank(vo.getStartTime()) || StringUtils.isBlank(vo.getEndTime())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/operationCardPassword";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			vo.setAuthType(LockAuthTypeEnum.CARD.getValue());
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("新增/修改卡,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);

		} catch (Exception e) {
			log.error("新增/修改卡异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 冻结/解冻卡
	 * 
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> freezeOrUnfreezeCard(SmartLockVO vo) {
		try {
			if (StringUtils.isBlank(vo.getSn()) || vo.getIndex() == null || vo.getSuspend() == null
					|| vo.getAuthInfoId() == null || vo.getCardType() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			if (!LockSuspendTypeEnum.operate(vo.getSuspend())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			// 冻结 冻结记录Id
			if (vo.getSuspend() == 1 && vo.getLockFrozenId() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}

			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/freezeOrUnfreezeCard";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			vo.setAuthType(LockAuthTypeEnum.CARD.getValue());
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("冻结/解冻卡,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			log.info("软库, 冻结结果, resultStr={}", resultStr);
			return ResultBean.successfulResult(callBackId);

		} catch (Exception e) {
			log.error("冻结/解冻卡异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 删除卡
	 * 
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> clearCard(SmartLockVO vo) {
		try {
			if (StringUtils.isBlank(vo.getSn()) || vo.getIndex() == null || vo.getAuthInfoId() == null
					|| vo.getCardType() == null) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/clearCard";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("删除卡,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);

		} catch (Exception e) {
			log.error("删除卡异常" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}

	/**
	 * 删除所有卡
	 * 
	 * @param vo
	 * @return 回调id
	 */
	public ResultBean<String> clearAllCard(@RequestBody SmartLockVO vo) {
		try {

			if (StringUtils.isBlank(vo.getSn())) {
				return ResultBean.failedResultOfParamWithMsg("参数错误");
			}
			String url = redisService.getValue(Constant.LOCK_URL);
			String accessKey = redisService.getValue(Constant.LOCK_ACCESS_KEY);
			String securityKey = redisService.getValue(Constant.LOCK_SECURITY_KEY);
			url = url + "lockAuth/V2/clearAllCard";
			vo.setAccessKey(accessKey);
			vo.setRandomStr(RandomNoUtils.getRandomNum(6));
			vo.setTimestamp(String.valueOf(System.currentTimeMillis()));
			String sign = RSAUtils.sign(vo.getTimestamp(), accessKey, vo.getRandomStr(), securityKey);
			log.info("删除所有卡,签名 ={}", sign);
			vo.setSign(sign);
			SSLNetProvider ssl = new SSLNetProvider();
			String resultStr = ssl.httpPost(url, JSONObject.toJSONString(vo));
			JSONObject result = JSONObject.parseObject(resultStr);

			int code = Integer.parseInt(result.get("code").toString());

			if (code != 0) {
				return ResultBean.failedResultWithMsg(result.getString("desc"));
			}
			JSONObject dataObj = result.getJSONObject("data");
			String callBackId = dataObj.get("callBackId").toString();
			redisService.putLockCallBack(vo.getSn() + "_" + callBackId, vo);
			return ResultBean.successfulResult(callBackId);
		} catch (Exception e) {
			log.error("删除所有卡" + e.getMessage(), e);
			return ResultBean.failedResultOfException();
		}
	}
}